<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>柱状图</title>
    <style>
        body, #map {
            position: absolute;
            width: 100%;
            height: 100%
        }
    </style>
    <script type="text/javascript" include="bootstrap" src="../js/include-web.js"></script>
    <script type="text/javascript" src="../../dist/include-mapboxgl.js"></script>
    <script type="text/javascript" src="../data/chinaConsumptionLevel.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript">
    var themeLayer, popup,
            url = (window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090") + "/iserver/services/map-china400/rest/maps/China_4326";
    var attribution = "Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> with <span>© <a href='http://iclient.supermapol.com' target='_blank'>SuperMap iClient</a></span> with <span>© <a href='https://www.mapbox.com' target='_blank'>MapBox</a></span>";
    var map = new mapboxgl.Map({
        container: 'map',
        style: {
            "version": 8,
            "sources": {
                "raster-tiles": {
                    "attribution": attribution,
                    "type": "raster",
                    "tiles": [url + '/zxyTileImage.png?z={z}&x={x}&y={y}'],
                    "tileSize": 256,
                },
            },
            "layers": [{
                "id": "simple-tiles",
                "type": "raster",
                "source": "raster-tiles",
                "minzoom": 0,
                "maxzoom": 22
            }]
        },
        center: [116.85, 39.79],
        zoom: 3
    });
    map.addControl(new mapboxgl.NavigationControl(), 'top-left');
    map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');

    map.on('load', function () {
        var features = [];
        for (var i = 0, len = chinaConsumptionLevel.length; i < len; i++) {
            // 省居民消费水平（单位：元）信息
            var provinceInfo = chinaConsumptionLevel[i];
            var geo = new mapboxgl.LngLat(provinceInfo[1], provinceInfo[2]);
            var attrs = {};
            attrs.NAME = provinceInfo[0];
            attrs.CON2009 = provinceInfo[3];
            attrs.CON2010 = provinceInfo[4];
            attrs.CON2011 = provinceInfo[5];
            attrs.CON2012 = provinceInfo[6];
            attrs.CON2013 = provinceInfo[7];
            var fea = new mapboxgl.supermap.ThemeFeature(geo, attrs);
            features.push(fea);
        }
        addThemeLayer();
        themeLayer.addFeatures(features);
    });

    function addThemeLayer() {
        themeLayer = new mapboxgl.supermap.GraphThemeLayer("RankSymbolLayer", "Bar",
                {
                    map: map,
                    attributions: " ",
                    themeFields: ["CON2009", "CON2010", "CON2011", "CON2012", "CON2013"],
                    opacity: 0.9,
                    chartsSetting: {
                        // width，height，codomain 分别表示图表宽、高、数据值域；此三项参数为必设参数
                        width: 240,
                        height: 100,
                        codomain: [0, 40000],       // 允许图表展示的值域范围，此范围外的数据将不制作图表
                        barStyle: {fillOpacity: 0.7},       // 柱状图中柱条的（表示字段值的图形）样式
                        barHoverStyle: {fillOpacity: 1},       //  柱条 hover 样式
                        xShapeBlank: [10, 10, 10],      // 水平方向上的空白间距参数
                        axisYTick: 4,         // y 轴刻度数量
                        axisYLabels: ["4万", "3万", "2万", "1万", "0"],         // y 轴标签内容
                        axisXLabels: ["09年", "10年", "11年", "12年", "13年"],         // x 轴标签内容
                        backgroundStyle: {fillColor: "#CCE8CF"},        // 背景样式
                        backgroundRadius: [5, 5, 5, 5],        // 背景框圆角参数
                        //阴影开关 默认是打开
                        showShadow: true,
                        //阴影样式
                        barShadowStyle: {
                            shadowBlur: 8,
                            shadowOffsetX: 2,
                            shadowOffsetY: 2,
                            shadowColor: "rgba(100,100,100,0.8)"
                        },
                        //按字段设置柱条样式[渐变开始颜色,渐变终止颜色]  与 themeLayer.themeFields 中的字段一一对应）
                        barLinearGradient: [["#00FF00", "#00CD00"], ["#00CCFF", "#5E87A2"], ["#00FF66", "#669985"], ["#CCFF00", "#94A25E"], ["#FF9900", "#A2945E"]]
                    }
                });

        //专题图层 mousemove 事件
        themeLayer.on('mousemove', showInfoWin);
    }

    function showInfoWin(e) {
        // e.target 是图形对象，即数据的可视化对象，柱状图中是柱条;
        // 图形对象的 refDataID 属性是数据（feature）的 id 属性，它指明图形对象是由那个数据制作而来;
        // 图形对象的 dataInfo 属性是图形对象表示的具体数据，他有两个属性，field 和 value;
        if (e.target && e.target.refDataID && e.target.dataInfo) {
            closeInfoWin();
            // 获取图形对应的数据 (feature)
            var fea = themeLayer.getFeatureById(e.target.refDataID);

            var info = e.target.dataInfo;

            // 弹窗内容
            var contentHTML = "<div style='color: #000; background-color: #fff'>";
            contentHTML += "省级行政区名称:<br><strong>" + fea.attributes.NAME + "</strong>";

            contentHTML += "<hr style='margin: 3px'>";
            switch (info.field) {
                case "CON2009":
                    contentHTML += "09年居民消费水平 <br/><strong>" + info.value + "</strong>（元）";
                    break;
                case "CON2010":
                    contentHTML += "10年居民消费水平 <br/><strong>" + info.value + "</strong>（元）";
                    break;
                case "CON2011":
                    contentHTML += "11年居民消费水平 <br/><strong>" + info.value + "</strong>（元）";
                    break;
                case "CON2012":
                    contentHTML += "12年居民消费水平 <br/><strong>" + info.value + "</strong>（元）";
                    break;
                case "CON2013":
                    contentHTML += "13年居民消费水平 <br/><strong>" + info.value + "</strong>（元）";
                    break;
                default:
                    contentHTML += "No Data";
            }
            contentHTML += "</div>";

            var tempPoint = map.unproject(new window.mapboxgl.Point(e.event.x, e.event.y));
            popup = new mapboxgl.Popup({closeOnClick: false})
                    .setLngLat([tempPoint.lng, tempPoint.lat])
                    .setHTML(contentHTML)
                    .addTo(map);
            return;
        }
        closeInfoWin();
    }

    // 移除地图弹窗
    function closeInfoWin() {
        if (popup) {
            popup.remove(map);
        }
    }

</script>
</body>
</html>
